package J.算法.买卖股票问题;

public class _309最佳买卖股票时机含冷冻期 {

    public int maxProfit(int[] prices) {
        int n = prices.length;
        int[][] dp = new int[n][2];
        dp[0][0] = 0;
        dp[0][1] = -prices[0];

        for (int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
            if (i-2<0){
                dp[i][1] = Math.max(dp[i-1][1],-prices[i]);
            }else {
                dp[i][1] = Math.max(dp[i-1][1],dp[i-2][0]-prices[i]);
            }
        }
        return dp[n-1][0];
    }

    public int maxProfit1(int[] prices) {
        int n = prices.length;
        int dp_i_0 = 0;
        int dp_i_1 = -prices[0];
        int dp_last = 0;

        for (int i = 1; i < n; i++) {
            int temp = dp_i_0;
            dp_i_0 = Math.max(dp_i_0,dp_i_1 + prices[i]);
            dp_i_1 = Math.max(dp_i_1,dp_last - prices[i]);
            dp_last = temp;
        }

        return dp_i_0;
//        int[][] dp = new int[n][2];
//        dp[0][0] = 0;
//        dp[0][1] = -prices[0];

//        for (int i = 1; i < n; i++) {
//            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1]+prices[i]);
//            if (i-2<0){
//                dp[i][1] = Math.max(dp[i-1][1],-prices[i]);
//            }else {
//                dp[i][1] = Math.max(dp[i-1][1],dp[i-2][0]-prices[i]);
//            }
//        }
//        return dp[n-1][0];
    }
}
